package com.peanut.baby.mvp.dingyue;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.text.format.DateUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.peanut.baby.AppConfig;
import com.peanut.baby.R;
import com.peanut.baby.model.MyTag;

import com.peanut.baby.util.PrefUtil;
import com.peanut.devlibrary.util.StringUtil;
import com.peanut.devlibrary.util.SysUtil;
import com.peanut.devlibrary.util.TimeUtil;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DingyueActivity extends Activity implements View.OnClickListener
        ,DingyueTagAdapter.OnitemClickedListener,DingyueContract.View{

    @BindView(R.id.tag_hot)
    TagFlowLayout tagHotLayout;
    @BindView(R.id.tag_recommend)
    TagFlowLayout tagRecommendLayout;
    @BindView(R.id.tag_subscribed)
    TagFlowLayout tagAlreadyLayout;


    @BindView(R.id.subscribe)
    TextView subscribe;
    @BindView(R.id.dingyue_close)
    ImageView close;

    @BindView(R.id.dingyue_refresh)
    TextView refresh;

    private List<String> tagsHot;
    private DingyueTagAdapter hotAdapter;

    private List<String> tagsRecommend;
    private DingyueTagAdapter rmdAdapter;

    private List<String> tagsSubscribe;
    private DingyueTagAdapter subscribeAdapter;


    private DingyuePresenter presenter;
    int screenWidth,screenHeight;
    public static void start(Context ctx){
        ctx.startActivity(new Intent(ctx, DingyueActivity.class));
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        screenWidth = getResources().getDisplayMetrics().widthPixels;
        screenHeight = getResources().getDisplayMetrics().heightPixels;
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        window.getDecorView().setPadding(0, 0, 0, 0);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (screenWidth * 0.9);
        lp.height = (int) (screenHeight * 0.8);
        // 这里还可以设置lp.x，lp.y在x轴，y轴上的坐标，只是这个位置是基于Gravity的
        window.setAttributes(lp);

        setContentView(R.layout.activity_dingyue);
        ButterKnife.bind(this);
        initView();

        presenter = new DingyuePresenter(this,this);

        savePref();

        loadData();
    }
    //保存本次显示时间
    private void savePref(){
        PrefUtil.saveLong(AppConfig.PrefConfig.PREF_LAST_DINGYUE_POP,System.currentTimeMillis());
        String monthlyDingyueCountKey = "monthlyDingyueCountKey_"+TimeUtil.getCurrentDate();
        int count = PrefUtil.getIntDefaultZero(monthlyDingyueCountKey);

        PrefUtil.saveInt(monthlyDingyueCountKey,count+1);
    }

    private void initView(){

        refresh.setOnClickListener(this);
        close.setOnClickListener(this);
        subscribe.setOnClickListener(this);
        tagsHot = new ArrayList<>();
        tagsRecommend = new ArrayList<>();
        tagsSubscribe = new ArrayList<>();

        hotAdapter = new DingyueTagAdapter(this,DingyueTagAdapter.SET_HOT,tagsHot);
        rmdAdapter = new DingyueTagAdapter(this,DingyueTagAdapter.SET_RECOMMEND,tagsRecommend);
        subscribeAdapter = new DingyueTagAdapter(this,DingyueTagAdapter.SET_SUBSCRIBED,tagsSubscribe);


        tagHotLayout.setAdapter(hotAdapter);
        tagRecommendLayout.setAdapter(rmdAdapter);
        tagAlreadyLayout.setAdapter(subscribeAdapter);

        hotAdapter.setListener(this);
        rmdAdapter.setListener(this);
        subscribeAdapter.setListener(this);

        subscribe.setOnClickListener(this);


    }

    private void loadData(){
        presenter.getRecommendTags();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dingyue_refresh:
                presenter.getHotTags();
                break;
            case R.id.subscribe:
                subscribe();
                break;
            case R.id.dingyue_close:
                finish();
                break;
        }
    }

    @Override
    public void OnitemClicked(int setType, int index) {
        switch(setType){
            case DingyueTagAdapter.SET_HOT:
                String tag = tagsHot.remove(index);
                tagsSubscribe.add(tag);
                hotAdapter.notifyDataChanged();
                subscribeAdapter.notifyDataChanged();
                break;
            case DingyueTagAdapter.SET_RECOMMEND:
                tagsRecommend.remove(index);
                rmdAdapter.notifyDataChanged();
                break;
            case DingyueTagAdapter.SET_SUBSCRIBED:
                tagsSubscribe.remove(index);
                subscribeAdapter.notifyDataChanged();
                
                break;
        }

    }


    @Override
    public void onHotTagsGet(boolean success, List<String> tags, String failReason) {
        if(success && tags != null){
            if(tags != null && !tags.isEmpty()){
                tagsHot.clear();
                tagsHot.addAll(tags);
                hotAdapter.notifyDataChanged();
                tagHotLayout.requestLayout();
            }
        }else{
            Toast.makeText(this,failReason,Toast.LENGTH_LONG).show();

        }
    }

    @Override
    public void onRecommendTagsGet(boolean success, MyTag tags, String failReason) {
        if(success && tags != null){
            onMyTagGet(tags);
        }else{
            Toast.makeText(this,failReason,Toast.LENGTH_LONG).show();
            finish();
        }
    }

    @Override
    public void onSubscribeResult(boolean success) {
        Toast.makeText(this,"订阅"+(success? "成功":"失败"),Toast.LENGTH_SHORT).show();
        if(success){
            finish();
        }
    }

    private void subscribe(){
        String result  = prepareSubscribeTag();
        if(StringUtil.isNullOrEmpty(result)){
            Toast.makeText(this,"请选择订阅的标签",Toast.LENGTH_SHORT).show();
            return;
        }
        presenter.subscribeTags(result);
    }

    private String prepareSubscribeTag(){
        StringBuffer sb = new StringBuffer();
//        if(tagsRecommend != null && !tagsRecommend.isEmpty()){
//            for(int i=0;i<tagsSubscribe.size();i++){
//                sb.append(tagsRecommend.get(i));
//                sb.append(",");
//            }
//        }
        if(tagsSubscribe != null && !tagsSubscribe.isEmpty()){
            for(int i=0;i<tagsSubscribe.size();i++){
                sb.append(tagsSubscribe.get(i));
                sb.append(",");
            }
        }
        return sb.toString();
    }

    void onMyTagGet(MyTag myTags){
        if(myTags.hot != null && !myTags.hot.isEmpty()){
            tagsHot.clear();
            tagsHot.addAll(myTags.hot);
            hotAdapter.notifyDataChanged();
        }
        if(myTags.rem != null && !myTags.rem.isEmpty()){
            tagsRecommend.clear();
            tagsRecommend.addAll(myTags.rem);
            rmdAdapter.notifyDataChanged();
        }

        if(myTags.my != null && !myTags.my.isEmpty()){
            tagsSubscribe.clear();
            tagsSubscribe.addAll(myTags.my);
            subscribeAdapter.notifyDataChanged();
        }


    }
}
